﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

//GoogleMapURL originally written by Ben Cottrell for THSMashup.
//Redistribution is allowed, but intended only for free usage (no commercial usage).

namespace GmapDirections
{
//This form is used as a container for the GoogleMapURL class, for testing purposes.    
    
    
    public partial class Form1 : Form
    {
        bool AllSelected = false;
        string firstLoc;
        string[] listLocs = new string[0];
        public Form1()
        {
            InitializeComponent();
            
        }

        

        private void addbtn_Click(object sender, EventArgs e)
        {
            if (addtxtbox.Text != "")
            {
                listBox1.Items.Add(addtxtbox.Text);
            }
        }

        private void btnget_Click(object sender, EventArgs e)
        {

            GoogleMapAPI gmap = new GoogleMapAPI();
                mapview.Navigate(gmap.GetSingleLocation(qtxt.Text));
            
            
        }

        private void listBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                if (listBox1.Items.Count != 0)
                {

                    if (listBox1.Items.Count > 0)
                    {
                        for (int i = listBox1.SelectedIndex; i < listBox1.SelectedItems.Count; i++)
                        {
                            listBox1.Items.RemoveAt(i);
                        }
                    }
                }
            }
            if (e.Modifiers == Keys.Control && e.KeyCode == Keys.A)
            {
                if (AllSelected == false)
                {
                    for (int i = 0; i < listBox1.Items.Count; i++)
                    {
                        listBox1.SelectedItems.Add(listBox1.Items[i]);
                        
                    }
                    AllSelected = true;
                }
                else
                {
                    listBox1.SelectedItems.Clear();
                    AllSelected = false;
                }
                
            }
        }

        private void btngetmulti_Click(object sender, EventArgs e)
        {
            GoogleMapAPI gmap = new GoogleMapAPI();

            listLocs = new string[listBox1.Items.Count];
            for (int i = 0; i < listBox1.Items.Count; i++)
            {
                if (i > 0)
                {
                    listLocs[i] = listBox1.Items[i].ToString();
                }
                else
                {
                    firstLoc = ((string)listBox1.Items[i]);
                }
            }
            mapview.Navigate(gmap.GetDirections(firstLoc, listLocs));
            
        }

       

        

        
    }
   
[Obsolete("Use GoogleMapAPI instead.  Kept just in case.", false)]
    public class GoogleMapURL
    {
        //Current GoogleMapURL
        public String CurrentURL { get; set; }
        public String CurrentViewType { get; set; }
        //View Types
        public string normalMap = "m";
        public string sattelite = "k";
        public string hybrid = "h";
        public string terrain = "p";
        public string NetworkLinkXML = "nl";
        public string JSONObj = "dragdir";
        
        //String URL formats
        public string gmapURLBasicNZ = "http://maps.google.com/maps?q={0}&&hnear=nz&&output=embed&&";
        public string gmapURLDirNZ = "http://maps.google.com/maps?saddr={0}&&daddr={1}&&t={2}&&output=embed";

        //Return URL functions
        public string GetSingleLocation(string query)
        {
            CurrentURL = string.Format(gmapURLBasicNZ, query);
            return string.Format(gmapURLBasicNZ, query);
        }
        public string GetDirectionsDef(string Fromstr, string[] ToDestArray)
        {
            string daddr = "";
            if (ToDestArray.Length >= 2)
            {
                for (int i = 0; i < ToDestArray.Length; i++)
                {
                    if (i > 0)
                    {
                        daddr = daddr + ToDestArray[i] + "+to:";
                    }
                    else
                    {
                        daddr = daddr + ToDestArray[i];
                    }

                }
            }
            else
            {
                daddr = ToDestArray[0];
            }
            CurrentURL = string.Format(gmapURLDirNZ, new object[] { Fromstr, daddr, "embed" });
            return string.Format(gmapURLDirNZ, new object[]{ Fromstr,daddr,this.normalMap });
        }
        public string GetDirections(string Fromstr, string[] ToDestArray, string OutputType)
        {
            string daddr = "";
            if (ToDestArray.Length >= 2)
            {
                for (int i = 0; i < ToDestArray.Length; i++)
                {
                    if (i > 0)
                    {
                        daddr = daddr + "to:" + ToDestArray[i] + "+";
                    }
                    else
                    {
                        daddr = daddr + ToDestArray[i];
                    }
                }
            }
            else
            {
                daddr = ToDestArray[0];
            }

            if (OutputType == "m" || OutputType == "k" || OutputType == "h" || OutputType == "p" || OutputType == "nl" || OutputType == "dragdir")
            {
                CurrentURL = string.Format(gmapURLDirNZ, new object[] { Fromstr, daddr, OutputType });
                CurrentViewType = OutputType;
                return string.Format(gmapURLDirNZ, new object[] { Fromstr, daddr, OutputType });
            }
            else
            {
                throw new Exception("Invalid output type: " + OutputType);
            }
            
            
        }
        

    }
    public class GoogleMapGeocode
    {
        public string geocodeAPIStr = "http://maps.googleapis.com/maps/api/geocode/output?{0}";
        
    }

    public class GoogleMapAPI
    {
        public Uri CurrrentURL { get; set; }
        
        //Default Constructor
        public GoogleMapAPI() { }
        
        //View types
        public string normalMap = "m";
        public string sattelite = "k";
        public string hybrid = "h";
        public string terrain = "p";
        public string NetworkLinkXML = "nl";
        public string JSONObj = "dragdir";
        
        //This was last specified from http://developers.google.com/maps/documentation/directions/
        public string dirformat = "http://maps.googleapis.com/maps/api/directions/xml?origin={0}&&destination={1}&&waypoints={2}&&sensor=false";
        public string singleformat = "http://maps.google.com/maps?q={0}";
        public string GetSingleLocation(string query)
        {
            return string.Format(singleformat, query);
        }
        public string GetDirections(string startaddr, string[] dest)
        {
            string firstDest = "";
            string tempStr = "";
            if (startaddr == "")
            {
                throw new Exception("Need to specify a start address");
            
            }
            if (dest == null)
            {
                throw new Exception("No destinations specified!");
            }
            firstDest = dest[0];
            for (int i = 0; i < dest.Length; i++)
            {
                if (i > 0)
                {
                    tempStr = tempStr + dest[i] + "|";
                }
                else
                {
                    tempStr = tempStr + dest[i];
                }

            }
            return string.Format(dirformat, new object[] { startaddr, firstDest, tempStr });
            
        }
    }
    
    
}
